package com.yidu.boss.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.boss.sys.domain.Custodian;
import com.yidu.boss.sys.domain.CustodianExample;
import com.yidu.boss.sys.service.impl.CustodianServiceImpl;
import com.yidu.boss.util.RandomString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.jsp.tagext.TryCatchFinally;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:si
 * @Date:2019/11/22
 * @Description:com.yidu.boss.sys.controller
 * @Version:1.0
 */
@Controller
@RequestMapping("/custodian/")
public class CustodianController {

    //定义业务层实现类
    @Autowired
    private CustodianServiceImpl custodianService;

    /**
     * 跳转管理人页面
     * @return
     */
    @RequestMapping("custodianJsp")
    public String custodianJsp(){
        return "custodian";
    }

    /**
     * 查询所有给基金表使用
     * @return
     */
    @RequestMapping("selectAllCustodian")
    @ResponseBody
    public List<Custodian> selectAll(){
        List<Custodian> custodianList =custodianService.selectAll();
        return custodianList;
    }

    /**
     * 查询所有管理人
     * @return
     */
    @RequestMapping("custodianlist")
    @ResponseBody
    public Object doSelectAllCustodian(Integer page,Integer limit){
        //System.out.println("管理人列表");
        //页面大小，页面
        PageHelper.startPage(page,limit);
        //根据条件查询管理人数据
       List<Custodian> custodianList=custodianService.selectByExample(null);
        //使用pageInfo包装查询后的结果，封装了详细的查询数据，其中参数是列表数据
        PageInfo<Custodian> custodianPageInfo =new PageInfo<Custodian>(custodianList);
        //列表数据
        List<Custodian> custodianData =custodianPageInfo.getList();
        //调用工具类的分页计数数据格式：map集合
        Map<String,Object> map =RandomString.pagingALG(custodianPageInfo,custodianPageInfo.getTotal());
        return map;
    }

    /**
     * 模糊查询管理人
     *
     * @param condition 模糊查询条件
     * @param page      页码
     * @param limit     数据条数
     * @return 返回前端数据格式
     */
    @RequestMapping("selectCustodianLike")
    @ResponseBody
    public Object selectLike(String condition, Integer page, Integer limit) {
        //System.out.println(condition);
        //判断模糊查询条件参数是否为空
        if(condition.equals("")){
            //为空显示所有列表页面
            Object o =doSelectAllCustodian(page,limit);
            return o;
        }
        //开始页
        PageHelper.startPage(page,limit);
        //调用业务层方法
        List<Custodian> custodianList =custodianService.selectSomeByMore(condition);
        PageInfo<Custodian>custodianPageInfo =new PageInfo<Custodian>(custodianList);
        //调用工具类的分页计数数据格式：map集合
        Map<String, Object> map = RandomString.pagingALG(custodianPageInfo,custodianPageInfo.getTotal());
        return map;
    }

    /**
     * 添加
     * @param custodian
     * @return
     */
    @RequestMapping("insertCustodian")
    @ResponseBody
    public String addCustodian(Custodian custodian){
        int i =custodianService.insertCustodian(custodian);
        if(i > 0){
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 修改
     * @param custodian
     * @return
     */
    @RequestMapping("updateCustodian")
    @ResponseBody
     public String updateCustodian(Custodian custodian){
         int i =custodianService.updateCustodian(custodian);
         if(i > 0){
             return "修改成功";
         }
        return "修改失败";
     }

    /**
     * 删除
     * @param
     * @return
     */
     @RequestMapping("deleteCustodian")
     @ResponseBody
     public String deleteCustodian(String[] ids) {
         //System.err.println("删除");
         try {

             int i = 0;
             for(String id:ids){
                 i += custodianService.deleteCustodian(id);
             }
             if(i > 0){
                 return "删除成功";
             }
         }catch (DataAccessException e){
             System.err.println("此主键有外键约束不能删除！！！");
         }

        return "删除失败";
     }



}
